package cn.b.sky.mes.andon.service.impl;

import java.util.List;

import org.apache.commons.lang3.StringUtils;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
import org.springframework.util.CollectionUtils;

import cn.b.sky.mes.andon.dao.AndonEventDao;
import cn.b.sky.mes.andon.dao.AndonEventProcessDao;
import cn.b.sky.mes.andon.model.AndonEvent;
import cn.b.sky.mes.andon.model.AndonEventProcess;
import cn.b.sky.mes.andon.model.condition.AndonEventCondition;
import cn.b.sky.mes.andon.service.AndonEventService;
import cn.b.sky.common.utils.DatabaseUtils;

/**
 * <strong>Title : AndonEventServiceImpl.java<br>
 * </strong> <strong>Package : cn.b.sky.mes.andon.service.impl<br>
 * </strong> <strong>Description : </strong>Andon事件定义业务实现类<br>
 * <strong>Create on : 2016年10月17日 下午1:39:46<br>
 * </strong>
 * <p>
 * 
 * @author ShiYing.Ke<br>
 * @version <strong>v1.0.0</strong><br>
 * <br>
 *          <strong>修改历史:</strong><br>
 *          修改人|修改日期|修改描述<br>
 *          -------------------------------------------<br>
 * <br>
 * <br>
 */
@Service("andonEventService")
public class AndonEventServiceImpl implements AndonEventService {

	private static final Logger LOG = LoggerFactory
			.getLogger(AndonEventServiceImpl.class);

	@Autowired
	AndonEventDao andonEventDao;
	
	@Autowired
	AndonEventProcessDao andonEventProcessDao;

	@Override
	public List<AndonEvent> selectAndonEventListByCondition(
			AndonEventCondition condition) {
		if (null == condition) {
			return null;
		}
		if (StringUtils.isBlank(condition.getOrderField())) {
			condition.setOrderField("CreateDateTime");
		}
		return andonEventDao.selectAndonEventListByCondition(condition);
	}

	@Override
	public int selectAndonEventTotalCount(AndonEventCondition condition) {
		if (null == condition) {
			return 0;
		}
		return andonEventDao.selectAndonEventTotalCount(condition);
	}

	@Override
	public AndonEvent selectAndonEvent(String siteCode, String eventCode) {
		if (StringUtils.isEmpty(siteCode) || StringUtils.isEmpty(eventCode)) {
			return null;
		}
		AndonEvent event = new AndonEvent();
		event.setSiteCode(siteCode);
		event.setEventCode(eventCode);
		return andonEventDao.selectAndonEvent(event);
	}

	@Override
	public String deleteAndonEvent(String siteCode, String code) {
		String message = null;
		try {
			andonEventDao.deleteAndonEvent(siteCode, code);
		} catch (Exception e) {
			LOG.error("{}", e);
			if ((DatabaseUtils.isConstraintsError(e.getMessage()))) {
				message = "dataBaseOperationPkFail.M";
			}
		}
		return message;
	}

	@Override
	public void saveAndonEvent(AndonEvent andonEvent) {
		// 保存暗灯事件
		andonEventDao.insertAndonEvent(andonEvent);
		// 保存流程关联信息
		if(!CollectionUtils.isEmpty(andonEvent.getAndonEventProcessList())){
			for(AndonEventProcess aep : andonEvent.getAndonEventProcessList()){
				andonEventProcessDao.insertAndonEventProcess(aep);
			}
		}
	}

	@Override
	public void updateAndonEvent(AndonEvent andonEvent) {
		// 更新暗灯事件
		andonEventDao.updateAndonEvent(andonEvent);
		// 清除流程关联信息
		andonEventProcessDao.deleteAndonEventProcessByEventCode(andonEvent.getEventCode());
		// 保存流程关联信息
		if(!CollectionUtils.isEmpty(andonEvent.getAndonEventProcessList())){
			for(AndonEventProcess aep : andonEvent.getAndonEventProcessList()){
				andonEventProcessDao.insertAndonEventProcess(aep);
			}
		}
	}

}
